﻿using FlickrNet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace FlickrHarmony {
	public partial class VerifyImagesForm : Form {
		// http://farm5.static.flickr.com/4145/5095129874_64eb8b9f06_b.jpg
		const string _pattern = @"farm\d\.static\.flickr\.com\/\d{3,4}\/(\d{9,10})_(\w{10})(_[smlb])?\.jpg";

		internal BindingList<Link> Links { get; private set; }

		public VerifyImagesForm() {
			InitializeComponent();
			this.Load += new EventHandler(VerifyImagesForm_Load);
		}

		#region Progress Bar

		void ShowStatus(string message) {
			this.RunInMainThread(() => {
				ProgressBar.Visible = true;
				ProgressBar.Value = 0;
				StatusLabel.Text = message + " …";
			});
		}

		void ShowStatus(string message, ProgressBarStyle style) {
			this.RunInMainThread(() => {
				ProgressBar.Style = style;
				this.ShowStatus(message);
			});
		}

		void ClearStatus() {
			this.RunInMainThread(() => {
				StatusLabel.Text = string.Empty;
				ProgressBar.Visible = false;
			});
		}

		#endregion

		void VerifyImagesForm_Load(object sender, EventArgs e) {
			LinkGrid.AutoGenerateColumns = false;
			LinkGrid.DataSource = this.Links = new BindingList<Link>();
			//grdLinks.Columns.Clear();
			LinkGrid.Columns.Add(new DataGridViewTextBoxColumn() {
				DataPropertyName = "Name",
				HeaderText = "Name",
				Width = (LinkGrid.Width - 115)
			});
			LinkGrid.Columns.Add(new DataGridViewCheckBoxColumn() {
				DataPropertyName = "IsValid",
				HeaderText = "Valid",
				ThreeState = true,
				TrueValue = true,
				IndeterminateValue = null,
				FalseValue = false,
				Width = 50
			});
			this.DoWork(this.VerificationWorker);
		}

		private void VerificationWorker_DoWork(object sender, DoWorkEventArgs e) {
			var re = new Regex(_pattern, RegexOptions.IgnoreCase);
			var match = re.Match(Plugin.Selection);
			int i = 1;

			this.ShowStatus("Identifying Photo Links", ProgressBarStyle.Marquee);

			while (match.Success) {
				this.RunInMainThread(() =>
					this.Links.Add(new Link() {
						ID = match.Groups[1].Value,
						BlogSecret = match.Groups[2].Value
					})
				);
				match = match.NextMatch();
			}
			this.ShowStatus("Verifying Links", ProgressBarStyle.Continuous);
			int total = this.Links.Count;

			if (total > 0) {
				foreach (var l in this.Links) {
					if (VerificationWorker.CancellationPending) { break; }
					l.Update(Plugin.Flickr.PhotosGetInfo(l.ID));
					if (!UpdateButton.Enabled && l.IsChanged) {
						this.RunInMainThread(() => UpdateButton.Enabled = true);
					}
					VerificationWorker.ReportProgress(i, total);
					i++;
				}
			} else {
				e.Cancel = true;
			}
		}

		void VerificationWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) {
			ProgressBar.Value = e.ProgressPercentage;
		}

		void VerificationWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
			this.ClearStatus();
			StatusLabel.Text = (UpdateButton.Enabled) ? "Some links are invalid" : "All links are valid";
		}

		void btnUpdate_Click(object sender, EventArgs e) {
			this.DialogResult = DialogResult.OK;
		}

		void btnCancel_Click(object sender, EventArgs e) {
			this.DialogResult = DialogResult.Cancel;
		}
	}
}